;(function(global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined'
    ? (module.exports = factory())
    : typeof define === 'function' && define.amd
    ? define(factory)
    : (global.app = factory())
})(this, function() {
  'use strict'

  var clientConfig

  function App() {
    window.onload = this.onload
  }

  /**
   * 设置客户端配置
   */
  App.prototype.setClientConfig = function(config) {
    clientConfig = config
  }

  /**
   * 获取服务端配置
   */
  App.prototype.getClientConfig = function() {
    return clientConfig
  }
  /**
   * window.onload
   */
  App.prototype.onload = function() {
    setActivedTab()
    setMenuToggle()
  }

  /**
   * 切换菜单事件
   */
  App.prototype.onChangeMenu = function(menuPath) {
    redirectTo(menuPath, null)
  }

  /**
   * 切换tab事件
   */
  App.prototype.onTabChange = function(tabPath) {
    redirectTo(null, tabPath)
    setActivedTab()
  }

  /**
   * 切换菜单Toggle事件
   */
  App.prototype.onToggleMenu = function() {
    setMenuToggle(!isMenuToggle())
  }

  App.prototype.onSwipeTab = function(index) {
    // 获取滚动容器与滚动组件
    var wrapper = document.querySelector('.qloud-submenu .tabs-wrapper')
    var scroll = document.querySelector('.qloud-submenu .tabs-scroll')
    // 计算偏移量
    var wrapperWidth = wrapper.offsetWidth
    var scrollWidth = scroll.offsetWidth
    var offsetWidth = wrapperWidth / 2
    var currentOffset = scroll.dataset.offset ? parseInt(scroll.dataset.offset) : 0
    var translateOffset = currentOffset + offsetWidth * index * -1
    var minOffset = wrapperWidth - scrollWidth > 0 ? (wrapperWidth - scrollWidth) * -1 : 0
    // 修正目标偏移量
    translateOffset = index > 0 ? Math.max(translateOffset, minOffset) : Math.min(0, translateOffset)
    // 执行位移动画
    currentOffset + scroll.setAttribute('style', 'transform: translateX(' + translateOffset + 'px);')
    // 存储位移值
    scroll.dataset.offset = translateOffset
  }

  App.prototype.requestFullScreen = function(element) {
    if (isFullScreen()) {
      exitFullScreen()
    } else {
      requestFullScreen(element || document.documentElement)
    }
  }

  function requestFullScreen(element) {
    if (element.requestFullscreen) element.requestFullscreen()
    else if (element.msRequestFullscreen) element.msRequestFullscreen()
    else if (element.mozRequestFullScreen) element.mozRequestFullScreen()
    else if (element.webkitRequestFullscreen) element.webkitRequestFullscreen()
  }

  function exitFullScreen() {
    if (document.exitFullscreen) document.exitFullscreen()
    else if (document.msExitFullscreen) document.msExitFullscreen()
    else if (document.mozCancelFullScreen) document.mozCancelFullScreen()
    else if (document.webkitExitFullscreen) document.webkitExitFullscreen()
  }

  function isFullScreen() {
    return (
      (document.fullScreenElement && document.fullScreenElement !== null) ||
      document.mozFullScreen ||
      document.webkitIsFullScreen
    )
  }

  /**
   * 菜单跳转
   * @param {*} menuPath
   * @param {*} tabPath
   */
  function redirectTo(menuPath, tabPath) {
    if (tabPath) {
      location.href = tabPath
      return
    } else {
      var query = getQueryString([{ key: 'collapse', value: clientConfig.collapse }])
      location.href = menuPath + query
    }
  }

  /**
   * 生成查询字符串
   * @param {*} values
   */
  function getQueryString(querys) {
    const result = querys
      .map(function(item) {
        return item.key + '=' + item.value
      })
      .join('&')

    return result ? '?' + result : ''
  }
  /**
   * 获取菜单Toggle状态
   */
  function isMenuToggle() {
    const collapse = clientConfig.collapse
    return JSON.parse(collapse === undefined ? false : collapse)
  }

  /**
   * 设置菜单Toggle状态
   * @param {*} isToggle
   */
  function setMenuToggle(isToggle) {
    if (isToggle === undefined) {
      isToggle = isMenuToggle()
    }

    clientConfig.collapse = isToggle
    const sidemenu = document.querySelector('.qloud-layout-sidemenu')

    var oldClass = isToggle ? 'normal' : 'mini'
    var newClass = isToggle ? 'mini' : 'normal'
    // 替换样式class
    sidemenu && sidemenu.classList.replace(oldClass, newClass)
  }

  /**
   * 设置当前激活tab
   */
  function setActivedTab() {
    const list = document.getElementsByClassName('qloud-submenu-item')
    list.forEach(function(item) {
      isActivedTab(item) ? item.classList.add('actived') : item.classList.remove('actived')
    })
  }

  /**
   * 是否当前激活tab
   * @param {*} item
   */
  function isActivedTab(item) {
    try {
      const href = item.getAttribute('data-url')
      return window.location.href.includes(href)
    } catch (ex) {
      return false
    }
  }

  return new App()
})
